$(function(){
var app = {
  init: function () {
    this.$mode2 = $('.mode-2');
    $('.iframe').css('height', "calc( 100vh - 191px)");
    this.bind();
  },
  bind: function () {
    this.$mode2.on('click', '.navbar li.li-2', $.proxy(this.toMenu, this));
    this.$mode2.on('click', '.navbar a.hover_next', $.proxy(this.menuCtr, this));
    this.$mode2.on('click', '.tabs li.nav-page-item', $.proxy(this.pageTabsClick, this));
    this.$mode2.on('click', '.tabs i.fa-times-circle', $.proxy(this.closePageTabs, this));
  },
  //关闭页签
  closePageTabs: function (e) {
    e.stopPropagation();
    var $this = $(e.currentTarget);
    var url = $this.data('url');
    var $ul = $this.parents('ul');
    var len = $ul.children('li').length;
    if (len == 1) {
      alert('最后一个页签不可关闭');
      return;
    }
    $this.parent().remove();
    $('.nav-page-container iframe[src="' + url + '"]').remove();
    $ul.children('li:last').click();
  },
  //page-tabs点击事件
  pageTabsClick: function (e) {
    var $this = $(e.currentTarget);
    if ($this.hasClass('active')) {
      return
    }
    var url = $this.data('url');
    $this.addClass('active').siblings('li').removeClass('active');
    $('.nav-page-container iframe[src="' + url + '"]').show().siblings('iframe').hide();
    if ($('a.down[data-href="' + url + '"]').parents('li.li-1').hasClass('active')) {
      $('a.down[data-href="' + url + '"]').parent().addClass('active').siblings().removeClass('active');
    }else {
      $('li.li-2.active').removeClass('active').parent().removeClass('active').css('height', 0);
      $('a.down[data-href="' + url + '"]').parent().addClass('active').parents('ul').siblings('a.hover_next').click();                
    }
    this.setIframeHeight();
  },
  
  setIframeHeight: function () {
  	var pageHeight = $(window).height();
  	var realHeight = pageHeight - 183;//减去头部和脚部高度
  	$('.iframe_content').height(realHeight);
  },
  //菜单展开/关闭
  menuCtr: function (e) {
    var $this = $(e.currentTarget);
    var $li = $this.parent();
    if ($li.hasClass('active')) {
      $li.removeClass('active');
      $this.siblings('ul').css('height', 0);
      $this.children('i.fas').removeClass('fa-angle-down').addClass('fa-angle-left');
    }else {
      $('li.li-1.active').children('ul').css('height', 0);
      $('li.li-1.active').children('a.hover_next').children('i.fas').removeClass('fa-angle-down').addClass('fa-angle-left');
      $('li.li-1.active').removeClass('active');
      $li.addClass('active');
      var len = $li.find('li').length;
      var height = len * 35;
      $li.children('ul').css('height', height);
      $this.children('i.fas').removeClass('fa-angle-left').addClass('fa-angle-down');
    }
  },
  //判断该页面是否在浏览器的最上层
  refresh: function(){
    if(parent != window){
      parent.location = window.location;
    }
  },
  //菜单点击事件
  toMenu: function (e) {
    var $this = $(e.currentTarget);
    var $tabs = $('.nav-page-tabs');
    var name = $this.children('a.down').text();
    var url = $this.children('a.down').data('href');
    if ($('li.nav-page-item[data-url="' + url + '"]').length > 0) {//如果已经打开过这个页，就不再新增而是跳转
      $('li.nav-page-item[data-url="' + url + '"]').click();
      return;
    }
    if ($('li.nav-page-item').length > 9) {
      alert('您打开的页面太多了,请关闭一些之后再打开新页面');
      return;
    }
    var li = '<li class="nav-page-item" data-url="' + url + '" data-title="' + name + '">' + name + '<i class="fa fa-times-circle" data-url="' + url + '"></i></li>';
    $tabs.append(li);
    var iframe = '<iframe class="iframe_content" frameborder="0" scrolling="yes" src="' + url + '" style="width: 100%;"></iframe>';
    $('.nav-page-container').append(iframe);
    $('li.nav-page-item:last').click();
  },
};

app.init();
});